var gulp = require('gulp');//gulp库
var webpack = require("webpack");//webpack库
var sass = require('gulp-sass');//处理sass
var gulpReplace = require("gulp-replace");//替换html中的字符
var postcss = require('gulp-postcss');//自动添加css前缀
var minifycss = require('gulp-minify-css');//压缩css
var uglify=require('gulp-uglify');//压缩js
var includefile = require('gulp-file-include');//html中插入文件
var fs=require('fs');//文件

var src="src";//开发目录
var dist="app";//生成目录


/*------------------默认任务----------------*/
gulp.task('default',function () {
    //return;
    createjs();
    gulp.run('html');
    gulp.run('sass');
    gulp.run('copy');
});


/*------------------css预编译----------------*/

gulp.task('sass', function(){
    gulp.src([src+'/sass/*.scss','!**/mobileui.scss','!**/common_ui.scss'])
        .pipe(sass())
        .pipe(postcss())
        .pipe(minifycss())
        .pipe(gulp.dest(dist+'/css'));
});


/*------------------处理html文件----------------*/

gulp.task('html',function(){
    gulp.src(src+"/**.html")
        .pipe(gulp.dest(dist+"/"));
});




/*------------------拷贝文件----------------*/
gulp.task('copy',function(){
    gulp.src([src+'/js/common/**']).pipe(gulp.dest(dist+'/js/'));
    gulp.src([src+'/img/**/*.*']).pipe(gulp.dest(dist+'/img/'));
    gulp.src([src+'/font/**/*.*']).pipe(gulp.dest(dist+'/font/'));
    gulp.src([src+'/font2/**/*.*']).pipe(gulp.dest(dist+'/font2/'));
});


/*------------------监听文件变化----------------*/
gulp.watch([src+"/js/common/*.*"],function(){
   // gulp.src([src+'/js/common/*.*']).pipe(uglify()).pipe(gulp.dest(dist+'/js/'));
   gulp.src([src+'/js/common/*.*']).pipe(gulp.dest(dist+'/js/'));
});


gulp.watch(src+"/sass/*.scss",['sass']);
gulp.watch(src+"/*.html",['html']);
gulp.watch([src+"/js/page/*.vue"],createjs);



/*------------------webpack处理js-------------------*/
function createjs(res){
    let filename="",entryList=[];
    if(res){
        filename=res.path.split('\\');
        filename= filename[filename.length-1];
    }

    if(res && filename.indexOf('.js')>-1){
        entryList.push(filename);
    }else{
        let tmpList=fs.readdirSync('./src/js/page');
        tmpList.forEach((v)=>{
                if(v.indexOf('.vue')>-1){
                entryList.push(v);
            }
        })
    }
    entryList.forEach((v)=>{
            webpack(webpackConfig(v),function(error){
            if(!error){
                console.log("["+new Date().toLocaleTimeString()+"]",'Finished',v);
            }else{
                console.log("["+new Date().toLocaleTimeString()+"]","Error",v,error);
            }
        });
    });
}



function webpackConfig(filename){
    let config={
        entry:__dirname+"/src/js/index.js",
        output:{
            path: __dirname+"/app/js/",
            filename:filename.split('.')[0]+".js"
        },
        module:{
            loaders:[
                {
                    test:/(\.vue)$/,
                    loader:'vue-loader',
                    //options:{extractCSS:true}
                },
                {test: /(\.js)$/,loader:'babel-loader',query:{presets: ['es2015']}}
            ]
        },
        plugins:[
            new webpack.optimize.UglifyJsPlugin(),
            new webpack.DefinePlugin({
                'VUEFILE':JSON.stringify('./page/'+filename.split('.')[0]+'.vue')
            })
        ]
    };
    return config;
}